/**
 * Writed by yura and mak
 */

package wz.legend.util;

import java.io.FilterWriter;
import java.io.IOException;
import java.io.Writer;

public class DwarWriter extends FilterWriter {

    public DwarWriter(Writer out) {
        super(out);
    }

    public void write(char b) throws IOException {
        char[] buf = new char[1];
        buf[0] = (char) b;
        write(buf, 0, 1);
    }

    public void write(char[] buf, int off, int len) throws IOException {
        String lineBreak = System.getProperty("line.separator");
        for (int i = 0; i < len; i++) {
            if ((buf[i] > '\u007f')) {
                // write \udddd
                out.write("{ASC 0x");
                String hex = Integer.toHexString(buf[i]);
                StringBuffer hex4 = new StringBuffer(hex);
                hex4.reverse();
                int length = 4 - hex4.length();
                for (int j = 0; j < length; j++) {
                    hex4.append('0');
                }
                for (int j = 0; j < 4; j++) {
                    out.write(hex4.charAt(3 - j));
                }
                out.write('}');
            } else
                out.write(buf[i]);
        }
    }

}
